package com.sulent.frame.web.handler;

import com.sulent.frame.support.entity.R;
import com.sulent.frame.support.exception.BaseException;
import com.sulent.frame.support.exception.BusinessException;
import com.sulent.frame.support.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.sql.SQLException;

/**
 * @ClassName GlobalExceptionHandler
 * @Description: TODO
 * @Author Submerge.
 * @Since 2024/1/30 14:41
 * @Version 1.0
 */
@Slf4j
@RequestMapping
@RestControllerAdvice
public class GlobalExceptionHandler {


    @ExceptionHandler(value = BusinessException.class)
    public R<String> BusinessException(BusinessException e) {
        e.printStackTrace();
        String message = e.getMessage();
        return ResultUtil.error("BusinessException:"+message);
    }

    @ExceptionHandler(value = BaseException.class)
    public R<String> BaseException(BaseException e) {
        e.printStackTrace();
        String message = e.getMessage();
        return ResultUtil.error("BaseException:"+message);
    }


    @ExceptionHandler(value = SQLException.class)
    public R<String> SQLException(SQLException e) {
        e.printStackTrace();
        String message = e.getMessage();
        return ResultUtil.error("SQLException:"+message);
    }

    //参数校验异常
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public R<String> MethodArgumentNotValidException(MethodArgumentNotValidException e){
        log.warn(e.getMessage());
        return ResultUtil.error("MethodArgumentNotValidException:"+e.getMessage());
    }
    //参数校验异常
    @ExceptionHandler(BindException.class)
    public R<String> BindException(BindException e){
        log.warn(e.getMessage());
        StringBuilder sb = new StringBuilder();
        for (ObjectError error : e.getBindingResult().getAllErrors()) {
            sb.append(error.getDefaultMessage());
        }
        return ResultUtil.error("BindException:"+sb.toString());
    }
    //参数校验异常
    @ExceptionHandler(ConstraintViolationException.class)
    public R<String> ConstraintViolationException(ConstraintViolationException e){
        log.warn(e.getMessage());
        StringBuilder sb = new StringBuilder();
        for (ConstraintViolation<?> violation : e.getConstraintViolations()) {
            sb.append(violation.getMessage());
        }
        return ResultUtil.error(sb.toString());
    }
    //全局异常
    @ExceptionHandler(Exception.class)
    public R<String> Exception(Exception e){
        log.warn(e.getMessage());
        return ResultUtil.error("服务器内部错误");
    }

}
